<template>
  <div class="login-container">
    <div class="login-con">
      <div style="width: 50%;text-align: center;    margin-top: 5rem;">
        <img
          style="width: 80%;"
          src="@/assets/login_images/login_left.png" />
      </div>
      <div style="width: 50%;">

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

          <div class="title-container">
            <img style="width: 45%;" src="@/assets/logo.png" />
          </div>

          <el-form-item prop="请输入用户名/手机号">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
            <el-input
              ref="username"
              v-model="loginForm.phone"
              placeholder="请输入手机号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <div>
            <el-row>
              <el-col :span="18">
                <el-form-item prop="请填写验证码" style="margin-bottom: 1rem;">
              <span class="svg-container">
                <i class="el-icon-message"></i>
              </span>
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.smsCode"
                    placeholder="请填写验证码"
                    name="password"
                    tabindex="2"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                  />
                  <span class="show-pwd" @click="showPwd"></span>

                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-button style="width: 100%;    height: 3.4rem;" @click="sendCode">获取验证码</el-button>
              </el-col>
            </el-row>

          </div>

          <div class="cq-mx1">
            <!--<el-checkbox v-model="rememberPassword" shape="square">记住密码</el-checkbox>-->
            <span></span>
            <el-button @click.native.prevent="psLogin">密码登入</el-button>
          </div>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">立即登入</el-button>

          <div class="cq-mx1">
            <span>没有账号？<span class="cq-mx1-sp" @click="register">去注册</span></span>
            <el-button @click="resetPassword">忘记密码</el-button>
          </div>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

  import {trace} from "../../utils/trace"

  export default {
    name: 'SMSLogin',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!validUsername(value)) {
          callback(new Error('Please enter the correct user name'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('The password can not be less than 6 digits'))
        } else {
          callback()
        }
      }
      return {
        // rememberPassword:false,   //记住密码 是否
        loginForm: {
          phone: '',
          smsCode: ''
        },
        loginRules: {
          phone: [
            {required: true, message: '请输入手机号', trigger: 'blur'},
            { pattern: /^1[3-9]\d{9}$/, message: '长度在11 个数字'}
          ],
          smsCode: [
            {message: '请输入短信验证码', trigger: 'blur'},
            {min: 4, max: 6, message: '长度在 4到 6 个字符', trigger: 'blur'}
          ],
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined
      }
    },
    mounted: function () {

    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      sendCode:function(){    //发送短信验证码
        let reg = /^1[345789]\d{9}$/
        if (!reg.test(this.loginForm.phone)) {
          new Error('请输入正确的11位手机号');
          return;
        }else {
          // this.ruleForm.isSend=true;
          this.$store.dispatch('user/smsCode', {phone:this.loginForm.phone}).then((data) => {
            trace("handleLogin", data);
            this.loginForm.smsCode=data.code;

            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      },
      register:function(){
        this.$router.push({ path:  '/register' })
      },
      resetPassword:function(){
        this.$router.push({ path:  '/reset' });
      },
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      psLogin(){

        this.$router.push({ path:  '/login' })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/SMSLogin', this.loginForm).then(() => {

              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          } else {

            return false
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray:black;
  $cursor: black;


  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {

    .login-con{
      background-color: white;
      box-shadow: 0px 0px 6px rgb(0 0 0 / 10%);
      border-radius: 12px;
      width: 1280px;
      position: relative;
      margin: auto 3rem;
      display: flex;
      align-items: center;
      padding: 5rem 1rem;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(0, 0, 0, 0.3);
      background: transparent;
      border-radius: 5px;
      color: black;
    }
  }
</style>

<style lang="scss" scoped>
  $bg:#4247e6;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  $main_color:#134bde;

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    background-image: url(../../assets/login_images/bg.jpg);
    background-size: 100% auto;
    background-repeat: round;
    display: flex;
    align-items: center;
    justify-content: center;


    .login-form {
      position: relative;
      max-width: 100%;
      margin-right: 2rem;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: black;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;
      margin-bottom: 3rem;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .cq-mx1{
      position: relative;
      display: flex;
      justify-content: space-between;
      margin-top: -1rem;
      margin-bottom: 2rem;
      width: 100%;

      .el-button{
        border: none;
        padding: 2px 5px;
        color: $main_color;
      }
      .cq-mx1-sp{
        color: $main_color;
        cursor: pointer;
      }
    }
  }
</style>
